//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package com.aidong.dao.wills;

import com.aidong.dao.base.OrderSet;
import com.aidong.dao.wills.BaseDao;
import com.aidong.model.wills.KyBespeak;
import com.aidong.model.wills.KyContract;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import org.springframework.stereotype.Repository;

@Repository
public class KyBespeakDao extends BaseDao<KyBespeak, Integer> {
    public KyBespeakDao() {
    }

    public boolean chkMemberBespeak(int classId, String memberCode) {
        return this.getByWhere("classId=:p1 and memberCode=:p2 and status>0", new Object[]{Integer.valueOf(classId), memberCode}) != null;
    }

    public List<KyBespeak> getListByClass(int classId) {
        String sqlww = "classId=:p1 and status>0";
        return this.queryList(sqlww, new Object[]{Integer.valueOf(classId)}, (new OrderSet()).asc("bespeakDate"), -1);
    }

    public void addBespeak(KyBespeak bespeak) {
        this.add(bespeak);
        if(bespeak.getClassId() != null) {
            String sql = "update KyTimeTable set bespeakQuantity=bespeakQuantity+1, updateTime=getdate() where classId=:p1";
            this.updateBySql(sql, new Object[]{bespeak.getClassId()});
        }

    }

    public void updateCancel(KyBespeak bespeak) {
        String sql = "update KyBespeak set status=0, canceType=:p1, cancelUser=:p2, cancelTime=:p3 where autoId=:p4";
        this.updateBySql(sql, new Object[]{bespeak.getCanceType(), bespeak.getCancelUser(), bespeak.getClassTime(), bespeak.getAutoId()});
        if(bespeak.getClassId() != null) {
            sql = "update KyTimeTable set bespeakQuantity=bespeakQuantity-1, updateTime=getdate() where classId=:p1";
            this.updateBySql(sql, new Object[]{bespeak.getClassId()});
        }

    }

    public void updateClear(KyBespeak bespeak, List<KyContract> contractList) {
        String sql = "update KyBespeak set status=1, contractCode=:p1, clearUser=:p2, clearTime=:p3 where autoId=:p4";
        this.updateBySql(sql, new Object[]{bespeak.getContractCode(), bespeak.getClearUser(), bespeak.getClearTime(), bespeak.getAutoId()});
        if(bespeak.getClassId() != null) {
            sql = "update KyTimeTable set clearQuantity=clearQuantity+1, updateTime=getdate() where classId=:p1";
            this.updateBySql(sql, new Object[]{bespeak.getClassId()});
        }

        Iterator i$ = contractList.iterator();

        while(i$.hasNext()) {
            KyContract contract = (KyContract)i$.next();
            sql = "update KyContract set useAmount=:p1, lastUseTime=:p2 where saleId=:p3";
            this.updateBySql(sql, new Object[]{contract.getUseAmount(), new Date(), contract.getSaleId()});
        }

    }
}
